package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.edu.desserthouse.model.Clerk;

import edu.nju.desserthouse.action.business.ClerkInfoBean;
import edu.nju.desserthouse.dao.ClerkDao;
import edu.nju.desserthouse.dao.ShopDao;

/**
 * Servlet implementation class ModifyClerkServlet
 */
@WebServlet("/modifyclerk.technician")
public class ModifyClerkServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	ClerkDao clerkDao;
	@EJB
	ShopDao shopDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyClerkServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		String edit_id = req.getSession().getAttribute("edit_id").toString();
		
		int position = Integer.valueOf(req.getParameter("position")).intValue();
		int shopno = Integer.valueOf(req.getParameter("shopno")).intValue();
		
		Clerk clerk = clerkDao.find("id", edit_id);
		
		if(clerk != null){
			clerk.setPrivilege(position);
			clerk.setBelong(shopno);
			clerkDao.updateByClerkId(clerk);
		}
		
		long num = shopDao.getShopNum();
		ClerkInfoBean clerks = new ClerkInfoBean();
		clerks.setClerkList(clerkDao.getClerkList());
		req.getSession().setAttribute("clerkInfo", clerks);
		req.getSession().setAttribute("shop_count", num);
		resp.sendRedirect("/desserthouseWEB/technician/clerkmanage.jsp");
	}
}
